<!--
 作者：ex_baiyc
 日期：2016-10-11
 功能：
 参数：tableTitle 页面的title
      thead 表格标题
      gridColumns 表格展现需要取的字段
      tableRows 数据集合
      tableHeight tablecontent容器所占屏幕的高度，例：父组件中通过 table-height="'60vh'"
 事件：点击thead对表格进行排序
-->
<template>
  <div class="table-view">
    <p class="table-title" v-if="tabletitle">{{tableTitle}}</p>
    <table class="table-head">
      <thead>
      <tr>
        <th v-for="item in thead"
            @click="sortBy(item)">
          {{item}}
        </th>
      </tr>
      </thead>
    </table>
    <div class="table-content" :style="{height:tableHeight}">
      <table>
        <tbody>
        <tr v-for="row in tableRows  | orderBy sortBykey sortOrders[sortKey]" :id="row.id">
          <td v-for="item in gridColumns"
              :class="[{'firstcol': $index == 0}]">
            <span v-if="row[item] === true"
                  @click="delete($parent.row.contrast_id, $parent.$index)"
                  class="joined"><icon type="success"></icon></span>
            <span v-if="row[item] === false"
                  @click="add($parent.row.category_id, $parent.row.model, $parent.row.brand, $parent.row.index)"
                  class="notjoined"><icon
              type="success"></icon></span>
            <span v-if="row[item] !== 'true' && row[item] !== 'false'" v-html="row[item]"></span>
          </td>
        </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script>
  export default{
    props: ['tableTitle', 'thead', 'gridColumns', 'tableRows', 'tableHeight'],
    data () {
      var sortOrders = {}
      this.gridColumns.forEach(function (key) {
        sortOrders[key] = 1
      })
      return {
        sortKey: '',
        sortOrders: sortOrders
      }
    },
    methods: {
      sortBykey: function (a, b) {
        return Math.floor(a[this.sortKey]) - Math.floor(b[this.sortKey])
      },
      sortBy: function (key) {
        var activeTheadIndex = 0
        for (var i = 0; i < this.thead.length; i++) {
          if (this.thead[i] === key) {
            activeTheadIndex = i
          }
        }
        this.sortKey = this.gridColumns[activeTheadIndex]
        this.sortOrders[this.gridColumns[activeTheadIndex]] = this.sortOrders[this.gridColumns[activeTheadIndex]] * -1
      }
    },
    events: {},
    ready: function () {
    },
    watch: {
    }
  }
</script>

<style lang="scss" scoped>
  .table-view {
    position: relative;
    box-sizing: border-box;
    font-size: 1rem;
    color: #999;

    .table-content{
      overflow-y: scroll;
    }

    p.table-title {
      padding-left: .2rem;
      width: 100%;
      text-align: left;
      font-size: .35rem;
      height: .5rem;
      line-height: .5rem;
      margin-top: .2rem;
    }

    table {
      width: 100%;

      thead {
        background: #BCDBFF;
        color: #999;
        font-size: .30rem;

        th{
          font-weight: 500;
        }
      }

      .firstcol{
        border-right: 2px solid #e5e5e5;
      }

      tr {

        height: .8rem;
        line-height: .8rem;

        td {
          text-align: center;
          font-size: .25rem;
          vertical-align: middle !important;
          padding: .1rem;
          vertical-align: middle;
          text-align: center;

          &.remove {
            color: transparent;
            display: block;
            width: .3rem;
            height: .3rem;
            line-height: .3rem;
            text-align: center;
            border-radius: 50%;
            background: #333333;

            &:after {
              content: '-';
              font-size: .5rem;
              color: #fff;
            }
          }

        }
      }
    }
    table {
      width: 100%;
      font-size: .20rem;
      border-collapse: collapse;
      border-spacing: 0;
    }
  }
</style>
